package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kcsj.domain.DelayedApplication;
import com.kcsj.service.DelayedApplicationService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
 * @author 傅立贤
 * 29th Nov. 2020
 * {
 *     "orderNo":"01"
 * }
 */
@WebServlet("/handle.ctl")
public class HandleApplicationOfDelayController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //将request body转换为JSON字串
        String req_json = JSONUtil.getJSON(request);
        if (req_json.equals("")) {
            JSONObject message = MessageHelper.addMessage("请处理延期申请");
            response.getWriter().println(message);
            return;
        }

        try {
            //将请求转为JSONobject对象
            DelayedApplication delayedApplication = JSON.parseObject(req_json, DelayedApplication.class);
            delayedApplication.setApplicationStatus("延期中");
            DelayedApplicationService.getInstance().update(delayedApplication);
            JSONObject message = MessageHelper.addMessage("延期申请已同意");
            response.getWriter().println(message);
        } catch (SQLException e) {
            e.printStackTrace();
            MessageHelper.dateBaseError();
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }
    /*{
        "orderNo":"01"
    }*/
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将request body转换为JSON字串
        String req_json = JSONUtil.getJSON(request);
        if (req_json.equals("")) {
            JSONObject message = MessageHelper.addMessage("请处理延期申请");
            response.getWriter().println(message);
            return;
        } try {
            //将请求转为JSONobject对象
            DelayedApplication delayedApplication = JSON.parseObject(req_json, DelayedApplication.class);
            delayedApplication.setApplicationStatus("已驳回");
            DelayedApplicationService.getInstance().update(delayedApplication);
            JSONObject message = MessageHelper.addMessage("延期申请已驳回");
            response.getWriter().println(message);
        } catch (SQLException e) {
            e.printStackTrace();
            MessageHelper.dateBaseError();
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }
}
